<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改账户密码</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f8f9fa;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 上层样式 - 顶部导航栏 */
        .top-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 30px;
            background: linear-gradient(135deg, #1a2a6c 0%, #2c3e50 100%);
            color: white;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }

        .back-btn {
            background-color: rgba(255,255,255,0.15);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .back-btn:hover {
            background-color: rgba(255,255,255,0.25);
            transform: translateY(-2px);
        }

        .page-title {
            font-size: 24px;
            font-weight: 700;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }

        .username {
            font-size: 18px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        /* 内容区域样式 */
        .content-container {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 30px;
        }

        .password-form-container {
            background-color: white;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 600px;
            padding: 40px;
        }

        .form-title {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
            font-size: 28px;
            font-weight: 700;
            position: relative;
        }

        .form-title::after {
            content: '';
            display: block;
            width: 80px;
            height: 4px;
            background: linear-gradient(to right, #1a2a6c, #2c3e50);
            margin: 15px auto 0;
            border-radius: 2px;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .form-input {
            width: 100%;
            padding: 14px 20px;
            border: 2px solid #e1e5eb;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-input:focus {
            border-color: #4a6cf7;
            box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.2);
            outline: none;
        }

        .password-toggle {

            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #7a7e8c;
            cursor: pointer;

        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 8px;
            display: none;
            align-items: center;
            gap: 8px;
        }

        .submit-btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #1a2a6c 0%, #2c3e50 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .submit-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 7px 15px rgba(26, 42, 108, 0.3);
        }

        .strength-meter {
            height: 5px;
            border-radius: 3px;
            margin-top: 10px;
            background-color: #e1e5eb;
            overflow: hidden;
        }

        .strength-fill {
            height: 100%;
            width: 0;
            transition: width 0.3s ease;
        }

        .strength-labels {
            display: flex;
            justify-content: space-between;
            margin-top: 8px;
            font-size: 12px;
            color: #95a5a6;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .password-form-container {
                padding: 30px 20px;
            }

            .top-bar {
                padding: 15px 20px;
            }

            .back-btn span {
                display: none;
            }

            .back-btn::after {
                content: '返回';
            }

            .username span {
                display: none;
            }
        }
    </style>
</head>
<body>
<!-- 上层：顶部导航栏 -->
<div class="top-bar">
    <!-- 返回按钮 -->
    <button class="back-btn" onclick="location.href='/clients/clientuserspace'">
        <i class="fas fa-arrow-left"></i>
        <span>返回主页</span>
    </button>

    <!-- 页面标题 -->
    <div class="page-title">修改账户密码</div>

    <!-- 用户名 -->
    <div class="username">
        <i class="fas fa-user"></i>
        <!-- 数据接口：后端需提供 username -->
        <span th:text="${username} ? ${username} : '用户'">用户名</span>
    </div>
</div>

<!-- 下层：密码修改表单 -->
<div class="content-container">
    <div class="password-form-container">
        <h2 class="form-title">修改密码</h2>

        <form id="passwordForm" th:action="@{/clients/clientuserpassword}" method="post">
            <!-- 原密码 -->
            <div class="form-group">
                <label class="form-label">
                    <i class="fas fa-lock"></i>
                    原密码
                </label>
                <div style="position: relative;">
                    <input type="password"
                           class="form-input"
                           id="currentPassword"
                           name="currentPassword"
                           required
                           placeholder="请输入您的当前密码">
                    <i class="fas fa-eye password-toggle" id="toggleCurrentPassword"></i>
                </div>
                <div class="error-message" id="currentPasswordError">
                    <i class="fas fa-exclamation-circle"></i>
                    <span>原密码不正确</span>
                </div>
            </div>

            <!-- 新密码 -->
            <div class="form-group">
                <label class="form-label">
                    <i class="fas fa-key"></i>
                    新密码
                </label>
                <div style="position: relative;">
                    <input type="password"
                           class="form-input"
                           id="newPassword"
                           name="newPassword"
                           required
                           placeholder="请输入您的新密码（至少8位）">
                    <i class="fas fa-eye password-toggle" id="toggleNewPassword"></i>
                </div>
                <div class="error-message" id="newPasswordError">
                    <i class="fas fa-exclamation-circle"></i>
                    <span>密码至少需要8位，包含字母和数字</span>
                </div>

                <div class="strength-meter">
                    <div class="strength-fill" id="passwordStrength"></div>
                </div>
                <div class="strength-labels">
                    <span>弱</span>
                    <span>中</span>
                    <span>强</span>
                </div>
            </div>

            <!-- 确认新密码 -->
            <div class="form-group">
                <label class="form-label">
                    <i class="fas fa-key"></i>
                    确认新密码
                </label>
                <div style="position: relative;">
                    <input type="password"
                           class="form-input"
                           id="confirmPassword"
                           name="confirmPassword"
                           required
                           placeholder="请再次输入新密码">
                    <i class="fas fa-eye password-toggle" id="toggleConfirmPassword"></i>
                </div>
                <div class="error-message" id="confirmPasswordError">
                    <i class="fas fa-exclamation-circle"></i>
                    <span>两次输入的密码不一致</span>
                </div>
            </div>

            <!-- 后端错误信息 -->
            <div class="error-message" th:if="${errorMessage}" style="display: flex; margin-top: -10px; margin-bottom: 20px;">
                <i class="fas fa-exclamation-triangle"></i>
                <span th:text="${errorMessage}"></span>
            </div>

            <button type="submit" class="submit-btn">
                <i class="fas fa-save"></i> 确认修改
            </button>
        </form>
    </div>
</div>

<script>
    // 密码可见性切换
    function setupPasswordToggle(inputId, toggleId) {
        const passwordInput = document.getElementById(inputId);
        const toggleIcon = document.getElementById(toggleId);
        toggleIcon.addEventListener('click', function() {
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                toggleIcon.classList.replace('fa-eye', 'fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                toggleIcon.classList.replace('fa-eye-slash', 'fa-eye');
            }
        });
    }
    // 设置密码强度检测
    function setupPasswordStrength() {
        const passwordInput = document.getElementById('newPassword');
        const strengthBar = document.getElementById('passwordStrength');

        passwordInput.addEventListener('input', function() {
            const password = passwordInput.value;
            let strength = 0;
            // 长度检查
            if (password.length >= 8) strength += 30;
            if (password.length >= 12) strength += 10;
            // 字符种类检查
            if (/[A-Z]/.test(password)) strength += 15;
            if (/[a-z]/.test(password)) strength += 15;
            if (/[0-9]/.test(password)) strength += 20;
            if (/[^A-Za-z0-9]/.test(password)) strength += 20;
            // 更新强度条
            strengthBar.style.width = Math.min(strength, 100) + '%';
            // 更新颜色
            if (strength < 40) {
                strengthBar.style.backgroundColor = '#e74c3c';
            } else if (strength < 70) {
                strengthBar.style.backgroundColor = '#f39c12';
            } else {
                strengthBar.style.backgroundColor = '#2ecc71';
            }
        });
    }
    // 表单验证
    function setupFormValidation() {
        const form = document.getElementById('passwordForm');
        const newPassword = document.getElementById('newPassword');
        const confirmPassword = document.getElementById('confirmPassword');
        const newPasswordError = document.getElementById('newPasswordError');
        const confirmPasswordError = document.getElementById('confirmPasswordError');
        // 新密码验证
        newPassword.addEventListener('blur', function() {
            const password = newPassword.value;
            if (password.length < 8) {
                newPasswordError.style.display = 'flex';
            } else {
                newPasswordError.style.display = 'none';
            }
        });
        // 确认密码验证
        confirmPassword.addEventListener('input', function() {
            if (newPassword.value !== confirmPassword.value) {
                confirmPasswordError.style.display = 'flex';
            } else {
                confirmPasswordError.style.display = 'none';
            }
        });
        // 表单提交验证
        form.addEventListener('submit', function(e) {
            let isValid = true;
            // 检查新密码长度
            if (newPassword.value.length < 8) {
                newPasswordError.style.display = 'flex';
                isValid = false;
            }
            // 检查密码匹配
            if (newPassword.value !== confirmPassword.value) {
                confirmPasswordError.style.display = 'flex';
                isValid = false;
            }
            if (!isValid) {
                e.preventDefault();
            }
        });
    }
    // 初始化
    document.addEventListener('DOMContentLoaded', function() {
        setupPasswordToggle('currentPassword', 'toggleCurrentPassword');
        setupPasswordToggle('newPassword', 'toggleNewPassword');
        setupPasswordToggle('confirmPassword', 'toggleConfirmPassword');
        setupPasswordStrength();
        setupFormValidation();

        // 显示后端错误（如果有）
        const errorMessage = document.querySelector('.error-message[th\\:if]');
        if (errorMessage && errorMessage.textContent.trim() !== '') {
            errorMessage.style.display = 'flex';
        }
    });
</script>
</body>
</html>